package com.fulu.game.bi.service.impl.product;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.bo.channel.EventListBO;
import com.fulu.game.bi.entity.dto.EventListDTO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.product.EventAnalyseListVO;
import com.fulu.game.bi.hbase.BehaviourEventStatDao;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BehaviourEventStatService {
    @Autowired
    private BehaviourEventStatDao behaviourEventStatDao;


    public static final Map<Integer, String> behaviourEventStatMap;

    static {
        behaviourEventStatMap = new HashMap<>();
        behaviourEventStatMap.put(QueryTypeStatusEnum.DAY.getType(), "behaviour_event_stat_day");
        behaviourEventStatMap.put(QueryTypeStatusEnum.WEEK.getType(), "behaviour_event_stat_week");
        behaviourEventStatMap.put(QueryTypeStatusEnum.MONTH.getType(), "behaviour_event_stat_month");
    }

    public PageInfo<EventAnalyseListVO> list(EventListDTO eventListDTO) {
        EventListBO eventListBO = new EventListBO();
        BeanUtils.copyProperties(eventListDTO, eventListBO);
        eventListBO.setPageNum((eventListBO.getPageNum() - 1) * eventListBO.getPageSize());
        eventListBO.setTableName(behaviourEventStatMap.get(eventListDTO.getQueryType()));
        List<EventAnalyseListVO> list = behaviourEventStatDao.list(eventListBO);
        Integer count = behaviourEventStatDao.count(eventListBO);
        list.stream().forEach(map -> {
            map.setQueryType(eventListDTO.getQueryType());
        });
        PageInfo<EventAnalyseListVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        return pageInfo;
    }

    public void listExport(EventListDTO eventListDTO) {
        String fileName = "事件分析列表";
        LinkedHashMap<String, Mapper<EventAnalyseListVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(eventListDTO.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("事件名称", v -> v.getEventName());
        map.put("触发用户数", v -> v.getTriggerUserNum() + "");
        map.put("触发次数", v -> v.getTriggerNum() + "");
        Querier querier = (num) -> list(eventListDTO);
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
